﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.Dts.Pipeline.Design;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using System.Windows.Forms;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using Microsoft.SqlServer.Dts.Runtime;

namespace SSISForSAPComponents
{
    public class SAPDataSourceEditGUi:IDtsComponentUI
    {
        IDTSComponentMetaData90 metaData90;
        
        public bool DataChanged;
        #region IDtsComponentUI 成员

        public void Delete(IWin32Window parentWindow)
        {
            
            
        }
        /// <summary>
        /// 打开编辑按钮
        /// </summary>
        /// <param name="parentWindow"></param>
        /// <param name="variables"></param>
        /// <param name="connections"></param>
        /// <returns></returns>
        public bool Edit(IWin32Window parentWindow, Microsoft.SqlServer.Dts.Runtime.Variables variables, Microsoft.SqlServer.Dts.Runtime.Connections connections)
        {

            try
            {
                if (metaData90.RuntimeConnectionCollection[0].ConnectionManagerID == null || metaData90.RuntimeConnectionCollection[0].ConnectionManagerID.Equals(string.Empty))
                {


                    System.Windows.Forms.MessageBox.Show("Connection Not be Set,please use the advanced editor", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }

                SAPConnectionManager manager = null;
                foreach (Microsoft.SqlServer.Dts.Runtime.ConnectionManager connection in connections)
                {
                    if (connection.ID.Equals(metaData90.RuntimeConnectionCollection[0].ConnectionManagerID))
                    {
                        manager = (SAPConnectionManager)connection.AcquireConnection(null);
                    }
                }


                com.sap.mw.jco.JCO.Client conn = com.sap.mw.jco.JCO.createClient(manager.CLIENT, manager.USER, manager.PASSWD, manager.Lang, manager.ASHOST, manager.SYSNR);
                SAPDataSourceEditUi gui = new SAPDataSourceEditUi(this.GetPropertyValue("rfcMame") as String, conn, this.metaData90, variables);
                if (gui.ShowDialog() == DialogResult.OK)
                {

                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                
                throw ex;
            }
        }

        
        public void Help(IWin32Window parentWindow)
        {
            //throw new NotImplementedException();
        }

        public void Initialize(Microsoft.SqlServer.Dts.Pipeline.Wrapper.IDTSComponentMetaData90 dtsComponentMetadata, IServiceProvider serviceProvider)
        {
            metaData90 = dtsComponentMetadata;
            
        }

        public void New(IWin32Window parentWindow)
        {
            // throw new NotImplementedException();
        }

        #endregion

        public void SetPropertyValue(string PropertyName, object Value)
        {

            for (int i = 0; i < this.metaData90.CustomPropertyCollection.Count; i++)
            {
                if (this.metaData90.CustomPropertyCollection[i].Name.Equals(PropertyName))
                {
                    
                        this.DataChanged = true;
                        this.metaData90.CustomPropertyCollection[i].Value = Value;
                    
                    return;
                }
            }

        }
        public object GetPropertyValue(string PropertyName)
        {
            for (int i = 0; i < this.metaData90.CustomPropertyCollection.Count; i++)
            {
                if (this.metaData90.CustomPropertyCollection[i].Name.Equals(PropertyName))
                {
                    return this.metaData90.CustomPropertyCollection[i].Value;
                }
            }
            return null;
        }
    }
}
